﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Jy123.Web.PersonalInfo
{
    public partial class PrivacySet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string msg = string.Empty;

            Jy123.Model.User_Info mUserInfo = new Jy123.Model.User_Info();
            Jy123.BLL.User_Info bllUserInfo = new Jy123.BLL.User_Info();
            mUserInfo = bllUserInfo.GetModel(userID);

            Jy123.Model.User_Setting mSet = new Jy123.Model.User_Setting();
            Jy123.BLL.User_Setting bllSet = new Jy123.BLL.User_Setting();
            mSet = bllSet.GetModel(userID);

            string strHidSubmit = Request.Form["Submit"];
            if (!string.IsNullOrEmpty(strHidSubmit))
            {
                string strPrivacySpace = Request.Form["PrivacySpace"];
                string strPrivacyFavourite = Request.Form["PrivacyFavourite"];
                string strPrivacyEducate = Request.Form["PrivacyEducate"];
                string strPrivacyFriends = Request.Form["PrivacyFriends"];
                string strPrivacyLeaveWord = Request.Form["PrivacyLeaveWord"];
                string strPrivacyMiniBlog = Request.Form["PrivacyMiniBlog"];
                string strPrivacyGroup = Request.Form["PrivacyGroup"];
                mSet.PrivSpace = Convert.ToInt32(strPrivacySpace);
                mSet.PrivFavourite = Convert.ToInt32(strPrivacyFavourite);
                mSet.PrivEducate = Convert.ToInt32(strPrivacyEducate);
                mSet.PrivFriends = Convert.ToInt32(strPrivacyFriends);
                mSet.PrivLeaveWord = Convert.ToInt32(strPrivacyLeaveWord);
                mSet.PrivMiniBlog = Convert.ToInt32(strPrivacyMiniBlog);
                mSet.PrivGroup = Convert.ToInt32(strPrivacyGroup);
                bllSet.Update(mSet);
                msg = "<script>$(function(){$.moonbox.ok('修改成功!');})</script>";
                //加入动态
                Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                mUserForState = bllUserForState.GetModel(userID);
                new Jy123.BLL.Public.State().CreateModifyUserState(mUserForState, null, (int)Jy123.BLL.Public.EnumConfig.StateActionType.ModifyUserInfo);
                //加入动态
            }
            

            StringBuilder sbHtml = new StringBuilder(Cache["user_privacyset"].ToString());//最终写出页面的大sb
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友


            sbHtml = sbHtml.Replace("$SpaceSelectoption$", PrivacySel(mSet.PrivSpace));
            sbHtml = sbHtml.Replace("$FavouriteSelectoption$",PrivacySel(mSet.PrivFavourite));
            sbHtml = sbHtml.Replace("$EducateSelectoption$", PrivacySel(mSet.PrivEducate));
            sbHtml = sbHtml.Replace("$FriendsSelectoption$", PrivacySel(mSet.PrivFriends));
            sbHtml = sbHtml.Replace("$LeaveWordSelectoption$", PrivacySel(mSet.PrivLeaveWord));
            sbHtml = sbHtml.Replace("$MiniBlogSelectoption$", PrivacySel(mSet.PrivMiniBlog));
            sbHtml = sbHtml.Replace("$GroupSelectOption$",PrivacySel(mSet.PrivGroup));
            Response.Write(sbHtml.ToString()+msg);
        }
        /// <summary>
        /// 隐私选项下拉表
        /// </summary>
        /// <param name="privacyValue">数据库中的隐私值</param>
        /// <returns>字符串</returns>
        private string PrivacySel(int privacyValue)
        {
            string selOption = Cache["user_privacyselectoption"].ToString();
            switch (privacyValue)
            {
                case 0:
                    selOption = selOption.Replace("$ForWholeSite$","selected='selected'")
                        .Replace("$ForNetWorkAndFriends$","")
                        .Replace("$ForFriends$","")
                        .Replace("$ForOwner$","");
                    break;
                case 1:
                    selOption = selOption.Replace("$ForWholeSite$", "")
                        .Replace("$ForNetWorkAndFriends$", "selected='selected'")
                        .Replace("$ForFriends$", "")
                        .Replace("$ForOwner$", "");
                    break;
                case 2:
                    selOption = selOption.Replace("$ForWholeSite$", "")
                        .Replace("$ForNetWorkAndFriends$", "")
                        .Replace("$ForFriends$", "selected='selected'")
                        .Replace("$ForOwner$", "");
                    break;
                case 3:
                    selOption = selOption.Replace("$ForWholeSite$", "")
                        .Replace("$ForNetWorkAndFriends$", "")
                        .Replace("$ForFriends$", "")
                        .Replace("$ForOwner$", "selected='selected'");
                    break;
            }
            return selOption;
        }
    }
}
